from __future__ import absolute_import
from src import const
from src.osm import Xapi
import argparse
import os
import logging
import sys


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--save-path', default='data/china')
    parser.add_argument('-r', '--region', default='china_jzh')
    parser.add_argument('-k', '--tag-keyword', default='landuse')
    parser.add_argument('-v', '--tag-value')
    return parser.parse_args()


def log_config():
    logging.basicConfig(level=logging.INFO)

def main(args):
    region = getattr(const, args.region.upper() + '_REGION')
    filename = os.path.join(args.save_path, args.region.lower() + '_' + args.tag_keyword + '_' + args.tag_value + '.txt')
    if os.path.exists(filename):
        command = raw_input('%s has already existed, continue? y/n:' % filename).strip().lower()
        if command != 'y':
            return
    xapi = Xapi()
    xapi.download_ways(filename, args.tag_keyword, args.tag_value, region)


if __name__ == '__main__':
    log_config()
    args = parse_args()
    main(args)

